from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

dr=webdriver.Chrome()
dr.maximize_window()
dr.get("http://passport2.hut.edu.cn/login?refer=http%3A%2F%2Fmooc1.hut.edu.cn""%2Fvisit%2Fc"
       "ourses%3Fs%3De56ee486c78937b3b5e78a56b6bd9b7d&fid=0&newversion=true&_blank=0")
sleep(10)
def wait_click(driver,by,value,time=10):
       try:
              a=WebDriverWait(driver,time).until(EC.element_to_be_clickable((by,value)))
              a.click()
              print("wait_click true1")
              return True
       except:
              print("wait_click false1")
              return False

def send_keys(driver,by,value,key,time=10):
       a=WebDriverWait(driver,time).until(EC.element_to_be_clickable((by,value)))
       a.send_keys(key)
       print("ipt输入成功")

try:
       if wait_click(dr,By.XPATH,value="//input[@class='ipt-tel']"):
              send_keys(dr, By.XPATH,value="//input[@class='ipt-tel']",key="17716767931")
              if wait_click(dr,By.XPATH,value="//input[@class='ipt-pwd']"):
                     send_keys(dr, By.XPATH, value="//input[@class='ipt-pwd']", key="123321wen")
                     a=WebDriverWait(dr,10).until(EC.element_to_be_clickable((By.XPATH,"//button[@id='loginBtn']")))
                     a.click()
                     wait_click(dr,By.XPATH,value="//a[@class='experience']")
                     WebDriverWait(dr, 10).until(lambda d: len(d.window_handles) > 1)
                     dr.switch_to.window(dr.window_handles[-1])
       input("按回车键退出...")

except Exception as e:
    print(f"发生异常: {e}")
    input("按回车键退出...")  # 发生错误时也等待用户确认

finally:
    # 可选：取消注释此行以在用户确认后关闭浏览器
    # dr.quit()
    pass